package com.hm.cms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.cms.mongo.domain.CmsColumn;
import com.hm.cms.mongo.domain.QCmsColumn;
import com.hm.cms.mongo.vo.CmsBaseInfoVo;
import com.hm.cms.mongo.vo.CmsColumnVo;
import com.hm.cms.share.repository.mongo.CmsColumnRepository;
import com.hm.cms.share.service.CmsBaseInfoService;
import com.hm.cms.share.service.CmsColumnService;
import com.hm.common.common.BeanUtil;
import com.hm.common.common.CommonUtil;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.model.PageInfo;
import com.mysema.query.types.expr.BooleanExpression;

/**
 * @author shishun.wang
 * @date 下午2:40:04 2016年8月9日
 * @version 1.0
 * @describe
 */
@Service
public class CmsColumnServiceImpl implements CmsColumnService {

	@Autowired
	private CmsColumnRepository cmsColumnRepository;

	@Autowired
	private CmsBaseInfoService cmsBaseInfoService;

	@Override
	public CmsColumnVo getCmsColumn(String id) {
		CmsColumn cmsColumn = cmsColumnRepository.findOne(id);
		if (null == cmsColumn) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		CmsColumnVo cmsColumnVo = (CmsColumnVo) BeanUtil.copyProperties(new CmsColumnVo(), cmsColumn);
		cmsColumnVo.setParentName("未知");
		if(CommonUtil.isNotEmpty(cmsColumnVo.getParentId())){
			CmsColumnVo columnVo = getCmsColumn(cmsColumnVo.getParentId());
			if(null != columnVo){
				cmsColumnVo.setParentName(columnVo.getName());
			}
		}
		
		return cmsColumnVo;
	}

	@Override
	public String addCmsColumn(CmsColumnVo cmsColumnVo, String operationUser) {
		CmsBaseInfoVo cmsBaseInfo = cmsBaseInfoService.getCmsBaseInfo(cmsColumnVo.getWeibsite());
		if (null == cmsBaseInfo) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "网站站点");
		}

		CmsColumn cmsColumn = new CmsColumn();
		{
			cmsColumn = (CmsColumn) BeanUtil.copyProperties(new CmsColumn(), cmsColumnVo);
			cmsColumn.setCreateTime(System.currentTimeMillis());
			cmsColumn.setCreateUser(operationUser);
			cmsColumn.setEnable(CommonUtil.isEmpty(cmsColumn.getEnable()) ? true : cmsColumn.getEnable());
			cmsColumn.setSinglePage(CommonUtil.isEmpty(cmsColumn.getSinglePage()) ? false : cmsColumn.getSinglePage());
			cmsColumn.setId(null);
		}

		return cmsColumnRepository.save(cmsColumn).getId();
	}

	@Override
	public String updateCmsColumn(CmsColumnVo cmsColumnVo, String operationUser) {
		CmsColumn cmsColumn = cmsColumnRepository.findOne(cmsColumnVo.getId());
		if (null == cmsColumn) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		cmsColumn = (CmsColumn) BeanUtil.copyProperties(cmsColumn, cmsColumnVo);

		return cmsColumnRepository.save(cmsColumn).getId();
	}

	@Override
	public String deleteCmsColumn(CmsColumnVo cmsColumnVo, String operationUser) {
		cmsColumnRepository.delete(cmsColumnVo.getId());
		return cmsColumnVo.getId();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<CmsColumnVo> queryCmsColumn(Integer pageNumber, Integer pageSize, String weibsite,String parenId) {
		PageInfo<CmsColumnVo> pageInfo = new PageInfo<CmsColumnVo>(pageNumber, pageSize);

		if (CommonUtil.isEmpty(weibsite)) {
			return pageInfo;
		}
		QCmsColumn qCmsColumn = QCmsColumn.cmsColumn;
		BooleanExpression predicate = qCmsColumn.weibsite.eq(weibsite);
		if (CommonUtil.isEmpty(parenId)) {
			predicate = predicate.and(qCmsColumn.parentId.isNull());
		} else {
			predicate = predicate.and("0".equals(parenId) ? predicate.and(qCmsColumn.parentId.isNull())
					: qCmsColumn.parentId.eq(parenId));
		}
		
		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				qCmsColumn.createTime.desc());
		Page page = cmsColumnRepository.findAll(predicate,pageable);

		List<CmsColumnVo> vos = new ArrayList<CmsColumnVo>();
		page.getContent().forEach(baseInfo -> {
			CmsColumnVo cmsColumnVo = (CmsColumnVo) BeanUtil.copyProperties(new CmsColumnVo(), baseInfo);
			cmsColumnVo.setParentName("未知");
			if(CommonUtil.isNotEmpty(cmsColumnVo.getParentId())){
				CmsColumnVo columnVo = getCmsColumn(cmsColumnVo.getParentId());
				if(null != columnVo){
					cmsColumnVo.setParentName(columnVo.getName());
				}
			}
			vos.add(cmsColumnVo);
		});

		return pageInfo.execute(page, vos);
	}

	@Override
	public List<Map<String, Object>> childsById(String parenId, String weibsite) {

		ArrayList<Map<String, Object>> columnTreeVos = new ArrayList<Map<String, Object>>();
		if (CommonUtil.isEmpty(weibsite)) {
			return columnTreeVos;
		}

		QCmsColumn qCmsColumn = QCmsColumn.cmsColumn;
		BooleanExpression predicate = qCmsColumn.weibsite.eq(weibsite);
		if (CommonUtil.isEmpty(parenId)) {
			predicate = predicate.and(qCmsColumn.parentId.isNull());
		} else {
			predicate = predicate.and("0".equals(parenId) ? predicate.and(qCmsColumn.parentId.isNull())
					: qCmsColumn.parentId.eq(parenId));
		}

		cmsColumnRepository.findAll(predicate).iterator().forEachRemaining(column -> {
			Map<String, Object> menu = new HashMap<String, Object>();
			{
				menu.put("id", column.getId());
				menu.put("name", column.getName());
				menu.put("isParent", (childsById(column.getId(), weibsite).size() > 0));
			}
			columnTreeVos.add(menu);
		});

		return columnTreeVos;
	}

}
